package com.example.mylibrary.util;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by fairplay on 2018/6/8.
 */

public class ThreadManager {
    private final int THREAD_FIXED = 20;
    private static ThreadManager sInstance = null;
    private ThreadPoolExecutor mExecutorService;

    private ThreadManager() {
        this.mExecutorService = new ThreadPoolExecutor(20, 20, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue());
        this.mExecutorService.allowCoreThreadTimeOut(false);
    }

    public static synchronized ThreadManager getInstance() {
        if(sInstance == null) {
            sInstance = new ThreadManager();
        }

        return sInstance;
    }

    public void submit(Runnable task) {
        this.mExecutorService.submit(task);
    }

    public Future<Integer> submit(Callable<Integer> task) {
        return this.mExecutorService.submit(task);
    }

    public void shutdown() {
        if(!this.mExecutorService.isShutdown()) {
            this.mExecutorService.shutdownNow();
        }

    }
}
